package cn.colat.micse.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 订单号生成
 */
@Slf4j
public class OrderNoUtils {

    // 时区
    private static final ZoneId TIME_ZONE = ZoneId.of("Asia/Shanghai");
    private static final AtomicInteger SEQ = new AtomicInteger(1000);
    private static final DateTimeFormatter DF_FMT_PREFIX = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    /**
     * 生成订单号
     *
     * @return 订单号
     */
    public static String nextOrderNo() {
        LocalDateTime dataTime = LocalDateTime.now(TIME_ZONE);
        if (SEQ.intValue() > 9990) {
            SEQ.getAndSet(1000);
        }
        return dataTime.format(DF_FMT_PREFIX) + SEQ.getAndIncrement();
    }
}